import { createRouter, createWebHistory } from 'vue-router'
import {LOCAL,SESSION} from "@/utils/catch.js";
import {useNowRouterStore} from "@/stores/nowRouterStore.js";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path:'/',
      name:'login',
      component: () => import('@/views/login/Login.vue'),
    },
    {
      path:'/main',
      name:'main',
      component: () => import('@/layout/Layout.vue'),
      children:[]
    }
  ]
})

router.beforeEach(async (to,from) => {

  const nowRouter = useNowRouterStore()
  if ((!LOCAL.get('token') || !SESSION.get('userLoginStatus') ) && to.path !== '/'){
    console.log('no login to ',to,from,LOCAL.get('token'),SESSION.get('userLoginStatus'))
    return {path:'/'}
  }
  //tabs标签栏配置
  nowRouter.setName(to.meta.name)
  to.meta.name === '首页' ? nowRouter.setIndexPath([]) : nowRouter.setIndexPath(to.meta.indexPath)
  if (nowRouter.findTags(to.meta.name) === -1 && to.fullPath !== '/'){
    nowRouter.addTags({
      name:to.meta.name,
      router:to.fullPath,
    })
  }
  console.log('nowRouter',nowRouter)
  console.log(to,from)
  if (to.matched.length ===0) return{path :to.path}
})


export default router
